Emacs 對於編輯 Vue.js 專案的支援
(use-package web-mode
:ensure t
:mode ("\\.html\\'" "\\.vue\\'")
:config
(setq web-mode-markup-indent-offset 2)
(setq web-mode-css-indent-offset 2)
(setq web-mode-code-indent-offset 2)
(setq web-mode-enable-current-element-highlight t)
(setq web-mode-enable-css-colorization t)
(set-face-attribute 'web-mode-html-tag-face nil :foreground "royalblue")
(set-face-attribute 'web-mode-html-attr-name-face nil :foreground "powderblue")
(set-face-attribute 'web-mode-doctype-face nil :foreground "lightskyblue")
(setq web-mode-content-types-alist
'(("vue" . "\\.vue\\'")))
(use-package company-web
:ensure t)
(add-hook 'web-mode-hook (lambda()
(cond ((equal web-mode-content-type "html")
(my/web-html-setup))
((member web-mode-content-type '("vue"))
(my/web-vue-setup))
)))
)
(defun my/web-vue-setup()
"Setup for js related."
(message "web-mode use vue related setup")
(setup-tide-mode)
(prettier-js-mode)
(flycheck-add-mode 'javascript-eslint 'web-mode)
(flycheck-select-checker 'javascript-eslint)
(my/use-eslint-from-node-modules)
(add-to-list (make-local-variable 'company-backends)
'(comany-tide company-web-html company-css company-files))
)
借用 lunaryorn 在 https://emacs.stackexchange.com/questions/21205/flycheck-with-file-relative-eslint-executable 中的作法
(defun my/use-eslint-from-node-modules ()
"Use local eslint from node_modules before global."
(let* ((root (locate-dominating-file
(or (buffer-file-name) default-directory)
"node_modules"))
(eslint (and root
(expand-file-name "node_modules/eslint/bin/eslint.js"
root))))
(when (and eslint (file-executable-p eslint))
(setq-local flycheck-javascript-eslint-executable eslint))))
(add-hook 'flycheck-mode-hook #'my/use-eslint-from-node-modules)
原先在 Vue/cli 產生的 package.json已經定義跟安裝了 plugin:vue/essential
"eslintConfig": {
"root": true,
"env": {
"node": true
},
"extends": [
"plugin:vue/essential",
"eslint:recommended"
],
"rules": {},
"parserOptions": {
"parser": "babel-eslint"
}
},
相關簡報: 簡報
相關程式: Github 請下載到 ~/.emacs.d 下啟動 Emacs 就會自動設定安裝套件
相關資訊: 我的部落格